package workflow.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.springside.core.dao.HibernateEntityDao;

import sysman.model.Org;
import sysman.model.User;

import workflow.model.ProcessDefinition;

import common.PageInfo;

public class ProcessDefinitionDao extends HibernateEntityDao<ProcessDefinition>{
	
	@Override
	public void save(Object o) {
		this.getHibernateTemplate().saveOrUpdate(o);
	}

	public ProcessDefinition findProcessDefinitionByName(String name) {
		String hql = "FROM ProcessDefinition WHERE name = '" + name + "'";
		List<ProcessDefinition> list = this.getHibernateTemplate().find(hql);
		if(list != null && !list.isEmpty()) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	public List<ProcessDefinition> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = " FROM ProcessDefinition po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String id = (String)hm.get("id");
		String preId = (String)hm.get("preId");
		String name = (String)hm.get("name");
		String type = (String)hm.get("type");
		String version = (String)hm.get("version");
		String creator = (String)hm.get("creator");
		String crtateDate = (String)hm.get("crtateDate");


		int count[] = {0, 0, 0, 0, 0, 0, 0};

		if(id != null && !"".equals(id.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(preId != null && !"".equals(preId.trim())){
			listHql += " AND preId =:preId";
			count[1] = 1;
		}

		if(name != null && !"".equals(name.trim())){
			listHql += " AND name LIKE:name";
			count[2] = 1;
		}

		if(type != null && !"".equals(type.trim())){
			listHql += " AND type =:type";
			count[3] = 1;
		}

		if(version != null && !"".equals(version.trim())){
			listHql += " AND version LIKE:version";
			count[4] = 1;
		}

		if(creator != null && !"".equals(creator.trim())){
			listHql += " AND creator =:creator";
			count[5] = 1;
		}

		if(crtateDate != null && !"".equals(crtateDate.trim())){
			listHql += " AND crtateDate =:crtateDate";
			count[6] = 1;
		}

		//listHql += " ORDER BY po.professionalId DESC";
		countHql = "SELECT COUNT(*) FROM ProcessDefinition p ";
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("id", Long.valueOf(id));
			queryCount.setLong("id", Long.valueOf(id));
		}

		if(count[1] == 1){
			queryList.setLong("preId", Long.valueOf(preId));
			queryCount.setLong("preId", Long.valueOf(preId));
		}

		if(count[2] == 1){
			queryList.setString("name", "%" + name + "%");
			queryCount.setString("name", "%" + name + "%");
		}

		if(count[3] == 1){
			queryList.setString("type", type);
			queryCount.setString("type", type);
		}

		if(count[4] == 1){
			queryList.setString("version", "%" + version + "%");
			queryCount.setString("version", "%" + version + "%");
		}

		if(count[5] == 1){
			queryList.setLong("creator", Long.valueOf(creator));
			queryCount.setLong("creator", Long.valueOf(creator));
		}

		if(count[6] == 1){
			queryList.setDate("crtateDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(crtateDate));
			queryCount.setDate("crtateDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(crtateDate));
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ProcessDefinition>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "SELECT COUNT(*) FROM ProcessInstance po WHERE po.proDefId IN(" + ids + ")";
			List<Long> size = this.getHibernateTemplate().find(hql);
			if(size.get(0) > 0) {
				isSuccessful = false;
			} else {
				hql = "DELETE FROM ProcessDefinition po WHERE po.id IN (" + ids + ")";
				this.getHibernateTemplate().bulkUpdate(hql);
				isSuccessful = true;
			}
		} catch (Exception e){
			e.printStackTrace();
			isSuccessful = false;
		}
		
		return isSuccessful;
	}
	
	public void save(List entities){
		this.getHibernateTemplate().saveOrUpdateAll(entities);
	}
}












